//
// C++ Implementation: qtaddinterface
//
// Description:
//
//
// Author: Willi Ballenthin <wilbal1087@gmail.com>, (C) 2008
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "activityeditwidget.h"
#include <QDebug>

// TODO:
// This class should not edit the model, esp when adding a new activity.
// consider the case when only 'cycling' activities are filtered, but a new
// run is added.  this activity should not be added to the model.


/**
* Constructor.
* @param interface A reference to a database interface which to use for submitting.
* @param parent The parent widget.
*/
ActivityEditWidget::ActivityEditWidget ( ActivityDbInterface* interface, QWidget *parent )
        : ActivityWidget ( interface, parent )
{
    ui.setupUi ( this );

    activity = new Activity(interface);

    connect ( ui.pb_save,    SIGNAL ( clicked() ), this, SLOT ( submit() ) );
    connect ( ui.pb_cancel,  SIGNAL ( clicked() ), this, SLOT ( cancel() ) );
    connect ( ui.pb_discard, SIGNAL ( clicked() ), this, SLOT ( discard() ) );
}

/**
* Slot that sets the current Activity as a previous Activity
* that needs to be updated/edited.
* @param activity The values of an activity that need to be edited.
*/
void ActivityEditWidget::edit ( Activity activity )
{
      this->setEnabled ( true );
    *(this->activity) = activity;
      updateUi();
}


/**
* Deletes the current activity from the database. Displays a dialog to be sure.
*/
void ActivityEditWidget::discard()
{
    qDebug() << "in discard";

    QMessageBox msgBox;
    msgBox.setStandardButtons ( QMessageBox::Yes | QMessageBox::No );
    msgBox.setText ( "Are you sure you want to discard this activity?" );
    switch ( msgBox.exec() )
    {
        case QMessageBox::Yes:
            if ( activity->getId() == Activity::NEW )
                // new activity.
            {

            }
            else
            {
                activity->discard ();
                emit updatedDb();
            }
            setStatusMessage( "Activity discarded." );
            updateUi();
            this->setEnabled ( false );
            break;
        case QMessageBox::No:
            // no was clicked
            break;
        default:
            // should never be reached
            break;
    }
}

/**
* Clears the data from the current Activity.
* If the activity is only being edited, not composed,
* the changes are dropped, but the old data is not deleted.
* If the Activity is new, then all previous data is deleted.
*/
void ActivityEditWidget::cancel()
{
    if ( activity->getId() == Activity::NEW )
        // new activity.
    {    }
    else // existing activity
    {    }

    updateUi();

    this->setEnabled ( false );
}


/**
* Collects the data from the input widgets and submits this data
* to the database.  If the Activity has not yet been inserted, then
* the data is inserted as a new activity. Else, the Activity is updated.
*/
void ActivityEditWidget::submit()
{
    activity->setDate ( ui.de_date->date() );
    activity->setTime ( ui.te_time->time() );

    activity->setDuration ( ui.te_duration->time() );

    activity->setDistance ( ui.dsb_distance->value() );
    activity->setEffort ( ui.sb_effort->value() );
    activity->setLocation ( ui.le_location->text() );
    activity->setType ( ui.cb_type->currentText() );
    activity->setRoute ( ui.le_route->text() );
    activity->setNotes ( ui.te_notes->toPlainText() );
    activity->setSplits ( ui.le_splits->text() );

    // whether to update or insert is taken care of
    // by the activity method
    activity->insert ();

    if ( activity->getId() == Activity::NEW )
        // new activity.
    {
        setStatusMessage( "New activity added." );
    }
    else
    {
        setStatusMessage( "Activity updated." );
    }

    emit updatedDb();
    this->setEnabled ( false );
}


/**
* Updates the data fields of the widget based on the current activity.
*/
void ActivityEditWidget::updateUi()
{
    ui.de_date     ->setDate ( activity->getDate() );
    ui.te_time     ->setTime ( activity->getTime() );

    QTime duration ( activity->getDuration().hour(),
                     activity->getDuration().minute(),
                     activity->getDuration().second() );

    ui.te_duration ->setTime ( duration );
    ui.dsb_distance->setValue ( activity->getDistance() );
    QString type   = activity->getType();
    int typeIndex  = ui.cb_type->findText ( type );
    ui.cb_type     ->setCurrentIndex ( typeIndex );
    ui.sb_effort   ->setValue ( activity->getEffort() );
    ui.le_route    ->setText ( activity->getRoute() );
    ui.le_location ->setText ( activity->getLocation() );
    ui.te_notes    ->setText ( activity->getNotes() );
    ui.le_splits   ->setText ( activity->getSplits() );
}


/**
* Slot that clears the label showing current status.
*/
void ActivityEditWidget::clearStatusMessage()
{
    ui.pb_save->setText("&Save");
    ui.pb_cancel->setText("&Cancel");
    ui.pb_discard->setText("&Discard");
}

/**
* Slot that publishes a string to the widget status area.
* The message is only displayed for 4 seconds before
* clearStatusMessage called.
*/
void ActivityEditWidget::setStatusMessage(QString string)
{
    ui.pb_save->setText(string);
    ui.pb_cancel->setText(string);
    ui.pb_discard->setText(string);
    QTimer::singleShot ( 4000, this, SLOT ( clearStatusMessage() ) );
}


